package com.cloudweb.oa.aop;

import com.cloudweb.oa.cache.DepartmentCache;
import com.cloudweb.oa.entity.Department;
import com.cloudweb.oa.security.AuthUtil;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.visual.FormDAO;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
@Aspect
public class DeptAspect {
    @Autowired
    AuthUtil authUtil;

    @Autowired
    DepartmentCache departmentCache;

    @Autowired
    HttpServletRequest request;

    @Pointcut("execution(  * com.cloudweb.oa.controller.DepartmentController.create(..))")
    private void create() {
    }

    @Pointcut("execution(  * com.cloudweb.oa.controller.DepartmentController.save(..))")
    private void save() {
    }

    @Pointcut("execution(  * com.cloudweb.oa.controller.DepartmentController.del(..))")
    private void delete() {
    }

    // 用于方法调用后
    @Around("create()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        String deptCode = args[0].toString();
        FormDb fd = new FormDb();
        fd = fd.getFormDb("dept_log");
        FormDAO fdao = new FormDAO(fd);
        fdao.setFieldValue("user_name", authUtil.getUserName());
        fdao.setFieldValue("created_at", new Date());
        fdao.setFieldValue("dept_code", deptCode);
        fdao.setFieldValue("old_name", args[1].toString());
        fdao.setFieldValue("name", args[1].toString());
        fdao.setFieldValue("op_type", 0);
        fdao.setCreator(authUtil.getUserName());
        fdao.setUnitCode(authUtil.getUserUnitCode());
        fdao.create();
        return joinPoint.proceed();
    }

    // 删除数据库记录
    @Around("delete()")
    public Object doAroundDel(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        String deptCode = args[0].toString();
        Department department = departmentCache.getDepartment(deptCode);

        FormDb fd = new FormDb();
        fd = fd.getFormDb("dept_log");
        FormDAO fdao = new FormDAO(fd);
        fdao.setFieldValue("user_name", authUtil.getUserName());
        fdao.setFieldValue("created_at", new Date());
        fdao.setFieldValue("dept_code", deptCode);
        fdao.setFieldValue("old_name", department.getName());
        fdao.setFieldValue("name", department.getName());
        fdao.setFieldValue("op_type", "2");
        fdao.setCreator(authUtil.getUserName());
        fdao.setUnitCode(authUtil.getUserUnitCode());
        fdao.create();
        return joinPoint.proceed();
    }

    @Around("save()")
    public Object doAroundSave(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        Department department = (Department)args[0];
        String oldName = (String)request.getAttribute("oldName");
        if (department.getName().equals(oldName)) {
            return result;
        }
        String deptCode = department.getCode();
        FormDb fd = new FormDb();
        fd = fd.getFormDb("dept_log");
        FormDAO fdao = new FormDAO(fd);
        fdao.setFieldValue("user_name", authUtil.getUserName());
        fdao.setFieldValue("created_at", new Date());
        fdao.setFieldValue("dept_code", deptCode);
        fdao.setFieldValue("old_name", oldName);
        fdao.setFieldValue("name", department.getName());
        fdao.setFieldValue("op_type", "1");
        fdao.setCreator(authUtil.getUserName());
        fdao.setUnitCode(authUtil.getUserUnitCode());
        fdao.create();
        return result;
    }
}
